package jk.spider.core.task.dispatch;

import jk.spider.core.SpiderController;
import jk.spider.core.task.WorkerTask;
import jk.spider.core.task.threading.WorkerThreadPool;

import org.apache.log4j.Logger;

public class DispatchSpiderTasks extends DispatcherTask {
	protected static final Logger log = Logger.getLogger(DispatchSpiderTasks.class);
	protected WorkerThreadPool spiders;
	protected SpiderController controller;
	protected int count = 0 ;

	public DispatchSpiderTasks(SpiderController controller, WorkerThreadPool spiders) {
		super(controller, spiders);
		this.controller = controller;
		this.spiders = spiders;
	}

	public int getType() {
		return WorkerTask.WORKERTASK_SPIDERTASK;
	}

	public void prepare() { }

	public void execute() {
		log.info("Spider task dispatcher running ...");
		while(running) {
			try {
//				log.info("Spider dispatcher count [ C:" + count ++ + " T: "+ controller.getContent().getScheduler().getPendingJob() +"] ");
				spiders.assign(controller.getContent().getSpiderTask());
			} catch (InterruptedException e) {
				log.warn("DispatchSpiderTasks InterruptedException -> ", e);
				running = false;
			}
		}
		log.info("Spider task dispatcher dying ...");
	}
}
